package com.zj.study.utils.util;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import java.util.Random;
import java.util.UUID;
import org.springframework.lang.NonNull;

/**
 * <p> 生成ID工具类 <p/>
 *
 * @author 赵赳
 * @since 2021/9/23 10:40
 */
public class IDUtils {

  private IDUtils() {
  }

  private final static Random RANDOM = new Random();

  /**
   * 随机字符串种子
   */
  private static final char[] RANDOM_CHAR_SEEDS = {
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
      'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
      '6', '7', '8', '9'
  };

  /**
   * 生成32位UUID
   *
   * @param hasChar 是否包含字符
   * @return 32位UUID
   */
  private String uuid32(boolean hasChar) {
    var uuid = UUID.randomUUID().toString();
    if (!hasChar) {
      uuid = uuid.replaceAll("-", "");
    }
    return uuid;
  }

  /**
   * 雪花算法生成唯一id
   *
   * @return 唯一id
   */
  @NonNull
  private static Long assignId() {
    return IdWorker.getId();
  }

  /**
   * 生成特定长度的随机字符串
   *
   * @param length 随机字符串长度
   * @return 随机字符串
   */
  public static String randomString(int length) {
    StringBuilder builder = new StringBuilder(length);
    for (int i = 0; i < length; i++) {
      builder.append(RANDOM_CHAR_SEEDS[RANDOM.nextInt(RANDOM_CHAR_SEEDS.length)]);
    }
    return builder.toString();
  }



}
